# Simple AJAX chess website - Base request handler
#
# Copyright (C) 2008  Hong Anh Khoa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os

from cgi import escape
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import users

class BaseRequestHandler(webapp.RequestHandler):
  def generate(self, template_name, template_values={}):
    """Generate takes renders and HTML template along with values
       passed to that template

       Args:
         template_name: A string that represents the name of the HTML template
         template_values: A dictionary that associates objects with a string
           assigned to that object to call in the HTML template.  The defualt
           is an empty dictionary.
    """

    # Construct the path to the template
    directory = os.path.dirname(__file__)
    directory = os.path.split(directory)[0]
    path = os.path.join(directory, 'templates', template_name)
        
    # Respond to the request by rendering the template
    return template.render(path, template_values, debug=True)


  def generate_page(self, template_values={}):
    # Generate the page with the page.tpl.html template.
    
    # Values for the template
    values = {
      'menu': self.menu if self.__dict__.has_key('menu') else self.get_menu(),
      'title': self.title if self.__dict__.has_key('title') else self.get_title(),
      'header': self.header if self.__dict__.has_key('header') else self.get_header(),
      'user': self.get_user_block(),
      'css': ['style.css'] + (self.css if self.__dict__.has_key('css') else self.get_css()),
      'js': ['jquery-1.2.6.js', 'presence.js'] + (self.js if self.__dict__.has_key('js') else self.get_js()),
      'inline_js': self.inline_js if self.__dict__.has_key('inline_js') else [],
      'errors': self.errors if self.__dict__.has_key('errors') else []
    }
    values.update(template_values);
    
    res = self.generate('page.tpl.html', values)
    
    return res


  def get_menu(self):
    
    # Return the default menu.
    # A list of (title, link) tuples.
    # Overriden by subclasses if needed.
    
    return [('Home', '/'), ('Current game', '/user/gamelist/current'), ('Replay finished games', '/user/gamelist/finished')]


  def get_title(self):
    
    # Return the default title. Overriden by subclasses if needed.
    return 'hcmus-chess'


  def get_header(self):
    
    # Return the default content in the header. Overriden by subclasses if needed.
    return ''


  def get_user_block(self):
    
    # Return the default user block. Overriden by subclasses if needed.
    user = users.get_current_user()
    
    if user:
      res = "Logged in as <i>%s</i>.<br />" % user.nickname()
      link = '<a href="%s">Logout</a>' % escape(users.create_logout_url('/'))
    else:
      res = "Not logged in.<br />"
      link = '<a href="%s">Login with a Google account</a>' % escape(users.create_login_url(self.request.path))
      
    return res + link
    
  
  def get_css(self):
    
    # Return the css files. Overriden by subclasses if needed.
    return []
  
  
  def get_js(self):
    
    # Return the list of js files. Overriden by subclasses if needed.
    return []
    
    
